import { NumberSub } from '@/utils';
import { CaretDownOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
import { InputNumber, Select, Tooltip } from 'antd';
import moment from 'moment';
import { priceToolTipInfo } from '../dictionary';
import styles from './index.less';

const tableColumns = (data, changeDeclare) => {
  return [
    {
      title: <div className={styles.virtualPeriod}>虚拟时段</div>,
      dataIndex: 'timeOrderName',
      key: 'timeOrderName',
      width: 120,
      align: 'left',
      fixed: 'left',
      render: (text, row, index) => {
        if (row.timeOrderName === row.timeOrder) {
          return {
            children: text,
            props: { colSpan: 2 },
          };
        }
        return <span className={styles.collectValue}>{text}</span>;
      },
    },
    {
      title: '时点',
      dataIndex: 'timeOrder',
      key: 'timeOrder',
      width: 120,
      align: 'center',
      fixed: 'left',
      render: (text, row, index) => {
        if (row.timeOrderName === row.timeOrder) {
          return {
            children: null,
            props: { colSpan: 0 },
          };
        }
        return text;
      },
    },
    {
      title: '累计上网电量',
      dataIndex: 'totalQuantity',
      key: 'totalQuantity',
      width: 120,
      align: 'center',
    },
    {
      title: (
        <div>
          限电率
          <Tooltip
            overlayStyle={{
              fontSize: 12,
              width: 161,
            }}
            arrowPointAtCenter
            title={priceToolTipInfo.powerLimitingRate}
          >
            <ExclamationCircleOutlined className={styles.icon} />
          </Tooltip>
        </div>
      ),
      dataIndex: 'limitRate',
      key: 'limitRate',
      width: 120,
      align: 'center',
    },
    {
      title: '预判',
      children: [
        {
          title: '预测出力',
          dataIndex: 'forecastPower',
          key: 'forecastPower',
          width: 120,
          align: 'center',
        },
        {
          title: '预测现货电价',
          dataIndex: 'forecastPrice',
          key: 'forecastPrice',
          width: 120,
          align: 'center',
        },
        {
          title: '预测全月上网电量',
          dataIndex: 'forecastMonthQuantity',
          key: 'forecastMonthQuantity',
          width: 120,
          align: 'center',
        },
      ],
    },
    {
      title: '已有持仓量',
      dataIndex: 'contractQuantity',
      key: 'contractQuantity',
      width: 120,
      align: 'center',
    },
    {
      title: '偏差量',
      dataIndex: 'deviationQuantity',
      key: 'deviationQuantity',
      width: 120,
      align: 'center',
    },
    {
      title: '交易限制',
      children: [
        {
          title: '可买入量',
          dataIndex: 'buyableQuantity',
          key: 'buyableQuantity',
          width: 120,
          align: 'center',
        },
        {
          title: '可卖出量',
          dataIndex: 'vendibilityQuantity',
          key: 'vendibilityQuantity',
          width: 120,
          align: 'center',
        },
        {
          title: (
            <div>
              参考价格1
              <Tooltip
                overlayStyle={{
                  fontSize: 12,
                  width: 161,
                }}
                arrowPointAtCenter
                title={priceToolTipInfo.referencePrice}
              >
                <ExclamationCircleOutlined className={styles.icon} />
              </Tooltip>
            </div>
          ),
          dataIndex: 'referencePrice1',
          key: 'referencePrice1',
          width: 120,
          align: 'center',
        },
        {
          title: (
            <div>
              参考价格2
              <Tooltip
                overlayStyle={{
                  fontSize: 12,
                  width: 161,
                }}
                arrowPointAtCenter
                title={priceToolTipInfo.referencePrice}
              >
                <ExclamationCircleOutlined className={styles.icon} />
              </Tooltip>
            </div>
          ),
          dataIndex: 'referencePrice2',
          key: 'referencePrice2',
          width: 120,
          align: 'center',
        },
      ],
    },
    {
      title: '交易策路',
      children: [
        {
          title: <div style={{ textAlign: 'center' }}>申报方向</div>,
          dataIndex: 'declareType',
          key: 'declareType',
          width: 130,
          render: (text, row, index) => {
            return (
              <div>
                <Select
                  disabled={
                    row.date !== moment().format('YYYY-MM-DD') ||
                    row.timeOrder === row.timeOrderName
                  }
                  defaultValue={row.declareType !== null ? row.declareType : '--'}
                  className={styles.selectBox}
                  suffixIcon={
                    <CaretDownOutlined
                    /* style={{
                        color: row.date == moment().format('YYYY-MM-DD') ? '#FFFFFF' : null,
                      }} */
                    />
                  }
                  size="small"
                  onChange={(value) => {
                    changeDeclare(row.id, 'declareType', value);
                  }}
                  options={[
                    {
                      value: 1,
                      label: '买入',
                    },
                    {
                      value: 2,
                      label: '卖出',
                    },
                  ]}
                />
              </div>
            );
          },
        },

        {
          title: <div style={{ textAlign: 'center' }}>申报价格</div>,
          dataIndex: 'declarePrice',
          key: 'declarePrice',
          width: 130,
          render: (text, row, index) => {
            return (
              <div>
                {/*  <InputNumber
                  precision={2}
                  className={styles.selectBox}
                  size="small"
                  min={20}
                  max={600}
                  defaultValue={row.declarePrice !== null ? row.declarePrice : '--'}
                  onChange={(e) => {
                    let value = e.target.value;
                    if (value === '' || !/^[-]?[\.\d]+$/.test(value)) return;
                    value = NumberSub(value, 2);
                    changeDeclare(row.id, 'declarePrice', value);
                  }}
                /> */}
                <InputNumber
                  precision={2}
                  className={styles.selectBox}
                  size="small"
                  min={0}
                  disabled={
                    row.date !== moment().format('YYYY-MM-DD') ||
                    row.timeOrder === row.timeOrderName
                  }
                  defaultValue={row.declarePrice ?? null}
                  onChange={(value) => {
                    if (value === null || isNaN(value)) return;
                    value = NumberSub(value, 2);
                    changeDeclare(row.id, 'declarePrice', value);
                  }}
                />
              </div>
            );
          },
        },
        {
          title: <div style={{ textAlign: 'center' }}>申报电量</div>,
          dataIndex: 'declareQuantity',
          key: 'declareQuantity',
          width: 130,
          render: (text, row, index) => {
            return (
              <div>
                {/* <InputNumber
                  precision={3}
                  className={styles.selectBox}
                  size="small"
                  min={20}
                  max={600}
                  defaultValue={row.declareQuantity !== null ? row.declareQuantity : '--'}
                  onBlur={(e) => {
                    let value = e.target.value;
                    if (value === '' || !/^[-]?[\.\d]+$/.test(value)) return;
                    value = NumberSub(value, 3);
                    changeDeclare(row.id, 'declareQuantity', value);
                  }}
                /> */}
                <InputNumber
                  precision={3}
                  min={0}
                  className={styles.selectBox}
                  size="small"
                  disabled={
                    row.date !== moment().format('YYYY-MM-DD') ||
                    row.timeOrder === row.timeOrderName
                  }
                  defaultValue={row.declareQuantity ?? null}
                  onChange={(value) => {
                    if (value === null || isNaN(value)) return;
                    value = NumberSub(value, 3);
                    changeDeclare(row.id, 'declareQuantity', value);
                  }}
                />
              </div>
            );
          },
        },
      ],
    },
    {
      title: '交易结果',
      children: [
        {
          title: '出清方向',
          dataIndex: 'clearingType',
          key: 'clearingType',
          width: 120,
          align: 'center',
        },
        {
          title: '出清价格',
          dataIndex: 'clearingPrice',
          key: 'clearingPrice',
          width: 120,
          align: 'center',
        },
        {
          title: '出清电量',
          dataIndex: 'clearingQuantity',
          key: 'clearingQuantity',
          width: 120,
          align: 'center',
        },
      ],
    },
  ];
};

export default tableColumns;
